package org.wejob.core.log.util;

import java.util.function.Supplier;

public class SystemTime implements Time {
    public SystemTime() {
    }

    public long milliseconds() {
        return System.currentTimeMillis();
    }

    public long nanoseconds() {
        return System.nanoTime();
    }

    public void sleep(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException var4) {
            Thread.currentThread().interrupt();
        }

    }

    public void waitObject(Object obj, Supplier<Boolean> condition, long deadlineMs) throws InterruptedException {
        synchronized(obj) {
            while(!(Boolean)condition.get()) {
                long currentTimeMs = this.milliseconds();
                if (currentTimeMs >= deadlineMs) {
                    throw new RuntimeException("Condition not satisfied before deadline");
                }

                obj.wait(deadlineMs - currentTimeMs);
            }

        }
    }
}
